package cn.yhbk.web.modules.manager.Controller;

import cn.hutool.core.bean.BeanUtil;
import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbClass;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbCollege;
import cn.yhbk.web.modules.manager.Pojo.VO.College.CollegeAdd;
import cn.yhbk.web.modules.manager.Pojo.VO.College.CollegePag;
import cn.yhbk.web.modules.manager.Pojo.VO.College.CollegeUpdate;
import cn.yhbk.web.modules.manager.Pojo.VO.Major.MajorPag;
import cn.yhbk.web.modules.manager.Service.ITbCollegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 学院表 前端控制器
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@Api(tags = "学院接口")
@Slf4j
@RestController
@RequestMapping("/manager/tbCollege")
public class TbCollegeController {

    @Autowired
    private ITbCollegeService iTbCollegeService;

    @ApiOperation("根据ID查询学院")
    @GetMapping("/getCollegeInfo")
    public CommonResult<TbCollege> getCollegeInfo(@RequestParam("id") Long id){
        log.info("查询学院,id:" + id);
        return CommonResult.success(iTbCollegeService.getById(id));
    }

    @ApiOperation("增加学院")
    @PostMapping("/saveCollege")
    public CommonResult<String> saveClass(@RequestBody CollegeAdd collegeAdd){
        log.info("增加学院，" + collegeAdd);
        TbCollege tbCollege = BeanUtil.copyProperties(collegeAdd, TbCollege.class);
        // TODO 学工办ID
        Long adminId = MyUtil.getCurrentAdminId();
        tbCollege.setStuWorkOfficeId(adminId);
        Boolean saveCollege = iTbCollegeService.save(tbCollege);
        if(!saveCollege){
            return CommonResult.failed("保存学院出错");
        }
        return CommonResult.success("保存成功");
    }

    @ApiOperation("修改学院")
    @PutMapping("/updateCollege")
    public CommonResult<String> updateClass(@RequestBody CollegeUpdate collegeUpdate){
        log.info("修改学院，" + collegeUpdate);
        TbCollege tbCollege = BeanUtil.copyProperties(collegeUpdate, TbCollege.class);
        tbCollege.setUpdateTime(LocalDateTime.now());
        Boolean updateCollege = iTbCollegeService.updateById(tbCollege);
        if(!updateCollege){
            return CommonResult.failed("修改学院出错");
        }
        return CommonResult.success("修改成功");
    }

    @ApiOperation("删除学院")
    @DeleteMapping("/deleteCollege/{collegeId}")
    public CommonResult<String> deleteCollege(@PathVariable("collegeId") Long collegeId){
        log.info("删除学院，id：" + collegeId);
        Boolean deleteCollege = iTbCollegeService.removeById(collegeId);
        if(!deleteCollege){
            return CommonResult.failed("删除学院出错");
        }
        return CommonResult.success("删除成功");
    }

    @ApiOperation("分页获取学院")
    @GetMapping("/getCollegeInfoPag")
    public CommonResult<CollegePag> getCollegeInfoPag(@RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                                     @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize){
        CollegePag collegeInfoPag = iTbCollegeService.getCollegeInfoPag(currentPage, pageSize);
        return CommonResult.success(collegeInfoPag);
    }


}
